﻿using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace MVCLD.Helpers
{
    public static class Helpers
    {

        #region URL / Website Methods
        public static string CheckForHttpInLink(string TheURL)
        {
            if (TheURL != "")
            {
                if (TheURL.IndexOf("www.") > 0)
                {
                    if (!(TheURL.IndexOf("http") > 0))
                    {
                        TheURL = "http://" + TheURL;
                    }
                }
            }
            return TheURL;
        }

        public static Boolean CheckValidURL(string theURL)
        {
            try
            {
                HttpWebRequest URLReq;
                HttpWebResponse URLResp;
                URLReq = (HttpWebRequest)WebRequest.Create(theURL);
                URLResp = (HttpWebResponse)URLReq.GetResponse();
                if (URLResp.StatusCode == HttpStatusCode.OK)
                {
                    return true;
                }
                else
                {
                    return false;                
                }
            }
            catch
            {
                return false;
            }
        }

        public static string ReturnWebPageHTML(string theURL)
        {
            try
            {
                String ThePage;
                WebClient objWebClient = new WebClient();
                UTF8Encoding objUTF8 = new UTF8Encoding();
                ThePage = objUTF8.GetString(objWebClient.DownloadData(theURL));
                return ThePage;
            }
            catch
            {
                return "";
            }
        }

        public static int QueryString(string paramName, int defaultValue)
        {
            int value;
            if (!int.TryParse(HttpContext.Current.Request.QueryString[paramName], out value))
                return defaultValue;
            return value;
        }
        #endregion

        #region Date Methods
        public static string FormatLongDate(DateTime TheDate)
        {
            DateTime rightNow = DateTime.Now;
            return TheDate.ToString("dd MMMM yyyy");
        } 
        #endregion

        #region String Methods
        public static string StringReverse(string str)
        {
            int len = str.Length;
            char[] arr = new char[len];
            for (int i = 0; i < len; i++)
            {
                arr[i] = str[len - 1 - i];
            }
            return new string(arr);
        }

        public static int CountWordsInString(string Text)
        {
            if (String.IsNullOrEmpty(Text))
            { return 0; }
            else
            {
                string tmpStr;
                tmpStr = Text.Replace("\t", " ").Trim();
                tmpStr = tmpStr.Replace("\n", " ");
                tmpStr = tmpStr.Replace("\r", " ");
                while (tmpStr.IndexOf("  ") != -1)
                    tmpStr = tmpStr.Replace("  ", " ");
                return tmpStr.Split(' ').Length;
            }
        }

        public static string ReturnAmountWordsFromString(string Text, int WordAmount)
        {
            string tmpStr;
            string[] StringArray;
            string tmpStrReturn = "";
            tmpStr = Text.Replace("\t", " ").Trim();
            tmpStr = tmpStr.Replace("\n", " ");
            tmpStr = tmpStr.Replace("\r", " ");

            while (tmpStr.IndexOf("  ") != -1)
            {
                tmpStr = tmpStr.Replace("  ", " ");
            }
            StringArray = tmpStr.Split(' ');

            if (StringArray.Length < WordAmount)
            {
                WordAmount = StringArray.Length;
            }
            for (int i = 0; i < WordAmount; i++)
            {
                tmpStrReturn += StringArray[i] + " ";
            }
            return tmpStrReturn;
        }

        public static string CovertBBCode(string strTextToReplace)
        {
            ////Define regex 
            Regex regExp;
            ////Regex for URL tag without anchor 
            regExp = new Regex(@"\[url\]([^\]]+)\[\/url\]");
            strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$1</a>");
            ////Regex for URL with anchor 
            regExp = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]");
            strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$2</a>");
            ////Image regex 
            regExp = new Regex(@"\[img\]([^\]]+)\[\/img\]");
            strTextToReplace = regExp.Replace(strTextToReplace, "<img src=\"$1\" />");
            ////Bold text 
            regExp = new Regex(@"\[b\](.+?)\[\/b\]");
            strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>");
            ////Italic text 
            regExp = new Regex(@"\[i\](.+?)\[\/i\]");
            strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>");
            ////Underline text 
            regExp = new Regex(@"\[u\](.+?)\[\/u\]");
            strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>");
            ////Font size
            regExp = new Regex(@"\[size=([^\]]+)\]([^\]]+)\[\/size\]");
            strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"font-size: $1px\">$2</span>");
            ////Font color
            regExp = new Regex(@"\[color=([^\]]+)\]([^\]]+)\[\/color\]");
            strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"color: $1\">$2</span>");
            return strTextToReplace;
        }

        public static string StripHTMLFromString(string htmlText)
        {
            return Regex.Replace(htmlText, "<[^>]*>", string.Empty);
        }
        #endregion

        #region Numeric
            public static bool IsNumeric(object Expression)
            {
                bool isNum;
                double retNum;
                isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
                return isNum;
            }
        #endregion

        #region General
            public static string ReturnUsersIPaddress()
            {
                return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }

            public static bool SendMail(string strFrom, string strTo, string strSubject, string strMessage)
            {
                try
                {
                    System.Net.Mail.SmtpClient MySmtpClient = new System.Net.Mail.SmtpClient("MAILHOST");
                    MySmtpClient.Credentials = new System.Net.NetworkCredential("USERNAME", "PASSWORD");
                    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(strFrom, strTo, strSubject, strMessage);
                    msg.IsBodyHtml = true;
                    MySmtpClient.Send(msg);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        #endregion

    }
}
